<template>
  <div class='container'>
    <el-button type="text" @click="open"></el-button>
    <el-dialog :title="showtitle" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">>
      <span>
        <!-- 1.绑定数据2.定义规则3.绑定规则(el-form:model  :riles, el-form-item prop) -->
        <el-form ref="formData" label-width="100px" :model="formData" :rules="rules">
          <el-form-item label="标题" prop="title">
            <el-input placeholder="输入面经标题" v-model="formData.title"></el-input>
          </el-form-item>
          <el-form-item label="内容" prop="articleBody">
            <quillEditor v-model="formData.articleBody"></quillEditor>
          </el-form-item>
          <el-form-item label="视频地址">
            <el-input placeholder="输入视频地址" v-model="formData.videoURL"></el-input>
          </el-form-item>
          <el-form-item :style="{ display: 'flex', justifyContent: 'end' }">
            <el-button type="primary" @click="onSubmit">确认</el-button>
            <el-button @click="handleClose">取消</el-button>
          </el-form-item>
        </el-form>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { add, update } from '../../api/hmmm/articles'
export default {
  name: 'articles-add',
  components: {
    quillEditor
  },
  data () {
    return {
      dialogVisible: false,
      direction: 'rtl',
      // 表单数据
      formData: {
        title: '',
        articleBody: '',
        videoURL: '',
        id: null
      },
      // 规则
      rules: {
        title: [{ required: true, message: '题干不能为空', trigger: 'blur' }],
        articleBody: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showtitle () {
      return this.formData.id ? '编辑文章' : '新增文章'
    }
  },
  methods: {
    handleClose () {
      this.dialogVisible = false
      this.formData = {
        title: '',
        articleBody: '',
        videoURL: '',
        id: null
      }
    },

    open (row) {
      this.dialogVisible = true
      if (row) {
        console.log(row)
        this.formData.videoURL = row.videoURL
        this.formData.id = row.id
        this.formData.title = row.title
        this.formData.articleBody = row.articleBody
      }
    },
    // 新增和修改
    async onSubmit () {
      await this.$refs.formData.validate()
      if (this.formData.id) {
        await update(this.formData)
        this.$message.success('修改成功')
      } else {
        await add(this.formData)
        this.$message.success('添加成功')
      }
      this.formData = {
        title: '',
        articleBody: '',
        videoURL: '',
        id: null
      }
      this.dialogVisible = false
      this.$emit('updateList')
    }
  }
}

</script>

<style scoped lang='less'>
::v-deep .el-dialog {
  height: 600px;
}

.quill-editor {
  ::v-deep .ql-editor {
    height: 200px;
  }
}
</style>
